package com.gzhryc.socket.mosquitto.log;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.gzhryc.common.logging.Logger;
import com.gzhryc.socket.SocketCallback;
import com.gzhryc.socket.SocketEvent;
import com.gzhryc.socket.SocketNode;
import com.gzhryc.socket.mosquitto.EmptyMqttNode;

public class ClientDisconnectedLogHandler extends MosquittoLogHandler{

	static Logger log = Logger.getLogger(ClientDisconnectedLogHandler.class);
	
	Pattern part = Pattern.compile("Client (.*) disconnected.*");
	
	public ClientDisconnectedLogHandler(MosquittoLogHandler nextHandler,SocketNode node,SocketCallback callback) {
		super(nextHandler,node,callback);
	}
	
	@Override
	public boolean execute(String logStr) {
		Matcher mat = part.matcher(logStr);
		if(mat.find()){
			String clientId = mat.group(1);
			log.debug("ClientId:"+clientId+", Disconnected!!!");
			final SocketEvent event = new SocketEvent(new EmptyMqttNode(clientId,null,null));
			mNode.runTask(new Runnable() {
				@Override
				public void run() {
					mCallback.connectionLost(event);
				}
			});
			return true;
		}
		return super.execute(logStr);
	}

}
